package com.arpa.ntocc.common.service.impl;

import cn.hutool.core.util.IdUtil;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.domain.dto.AreaCountyDTO;
import com.arpa.ntocc.common.domain.entity.AreaCounty;
import com.arpa.ntocc.common.domain.vo.AreaCountyVO;
import com.arpa.ntocc.common.mapper.AreaCountyMapper;
import com.arpa.ntocc.common.service.IAreaCountyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 区/县信息表 服务实现类
 * </p>
 *
 * @author LEO
 * @since 2019-12-04
 */
@Service
public class AreaCountyServiceImpl extends ServiceImpl<AreaCountyMapper, AreaCounty> implements IAreaCountyService {

    /**
     * 保存
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor={Exception.class})
    public boolean save(AreaCounty entity) {
        if(StringUtils.isBlank(entity.getCountyCode())){
            entity.setCountyCode(IdUtil.simpleUUID());
        }
        if(StringUtils.isBlank(entity.getCreatedBy())){
            entity.setCreatedBy(UserUtil.getCode());
        }
        return super.save(entity);
    }

    /**
     * 根据code更新实体
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor={Exception.class})
    public int updateByCode(AreaCounty entity) {
        if(StringUtils.isBlank(entity.getModifiedBy())){
            entity.setModifiedBy(UserUtil.getCode());
        }
        return baseMapper.update(entity,new QueryWrapper<AreaCounty>().lambda().eq(AreaCounty::getCountyCode,entity.getCountyCode()));
    }

    /**
     * 查询列表
     * @param areaCountyDTO
     * @return
     */
    @Override
    public List<AreaCountyVO> queryList(AreaCountyDTO areaCountyDTO){
        List<AreaCountyVO> areaCountyVOList = baseMapper.queryList(areaCountyDTO);
        return areaCountyVOList;
    }

    /**
     * 查询合计,包含总数
     * @param areaCountyDTO
     * @return
     */
    @Override
    public AreaCountyVO queryListSum(AreaCountyDTO areaCountyDTO){
        AreaCountyVO areaCountyVO = baseMapper.queryListSum(areaCountyDTO);
        return areaCountyVO;
    }

}
